package com.wlr.study.leetcode;

public class LeetCode122 {
    public static int maxProfit(int[] prices) {
        boolean canTran = true;
        int buyPrice = 0;
        int count = 0;

        for(int i = 0; i < prices.length; i++){
            if(i+1 < prices.length && prices[i] < prices[i+1] && canTran){
                buyPrice = prices[i];
                canTran = false;
            }
            if(i+1 < prices.length && prices[i] > prices[i+1] && !canTran){
                count += (prices[i] - buyPrice);
                canTran = true;
            }
        }

        if(!canTran){
            count+=(prices[prices.length-1] - buyPrice);
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(LeetCode122.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
}
